package com.luo.gmall.pms.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.luo.gmall.pms.entity.AttrEntity;
import com.luo.gmall.pms.service.AttrService;
import com.luo.gmall.common.bean.PageResultVo;
import com.luo.gmall.common.bean.ResponseVo;
import com.luo.gmall.common.bean.PageParamVo;

/**
 * 商品属性
 *
 * @author zoro
 * @email 1160848022@qq.com
 * @date 2021-11-01 19:22:15
 * http://api.gmall.com/pms/attr/group/12
 */
@Api(tags = "商品属性 管理")
@RestController
@RequestMapping("pms/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;


    /**
     * 查询分类下的规格参数
     * @param cid 类别编号
     * @param type 属性类型
     * @param searchType 是否索引
     * @return 返回的规格列表
     */
    @GetMapping("category/{cid}")
    public ResponseVo<List<AttrEntity>> queryAttrCategoryByCid(@PathVariable("cid") Long cid, @RequestParam(value = "type",required = false) Integer type,  @RequestParam(value = "searchType",required = false)Integer searchType) {

        List<AttrEntity> attrEntities = attrService.queryAttrCategoryByCid(cid,type,searchType);
        return ResponseVo.ok(attrEntities);

    }


    /**
     *  查询组下的规格参数
     *
     * @param groupId 分组id
     * @return 分组id对应的数据
     */
    @GetMapping("group/{groupId}")
    public ResponseVo<List<AttrEntity>> queryAttrByGroupId(@PathVariable("groupId") Long groupId) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id", groupId);
        List<AttrEntity> entities = attrService.list(queryWrapper);
        return ResponseVo.ok(entities);
    }

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryAttrByPage(PageParamVo paramVo) {
        PageResultVo pageResultVo = attrService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<AttrEntity> queryAttrById(@PathVariable("id") Long id) {
        AttrEntity attr = attrService.getById(id);

        return ResponseVo.ok(attr);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody AttrEntity attr) {
        attrService.save(attr);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody AttrEntity attr) {
        attrService.updateById(attr);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids) {
        attrService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
